package com.sc.ktapplication.common.base

import android.app.ProgressDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

abstract class BaseFragment : Fragment() {

    private var mProgressDialog : ProgressDialog?=null

    abstract fun getLayoutId() : Int

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(getLayoutId(), container, false)
    }

    fun showLoadingDialog(){
        showLoadingDialog(null, true)
    }

    fun showLoadingDialog(msg: String?, cancelable: Boolean){
        initProgressDialog()
        if(!mProgressDialog!!.isShowing && !activity!!.isFinishing){
            mProgressDialog!!.setMessage(msg ?: "正在加载，请稍后...")
            mProgressDialog!!.setCancelable(cancelable)
            mProgressDialog!!.show()
        }
    }

    private fun initProgressDialog(){
        if(null == mProgressDialog){
            mProgressDialog = ProgressDialog(context)
        }
    }

    open fun dismissLoadingDialog() {
        if (null != mProgressDialog && mProgressDialog!!.isShowing) {
            mProgressDialog!!.dismiss()
        }
    }
}